
angular
    .module('Caltech', [
        'ui.router', 'ngAnimate', 'chieffancypants.loadingBar', 'ui.select2',
        'restangular', 'ngTable', 'xeditable', 'toaster', 'ngTableResizableColumns',
        'ui.bootstrap', 'Caltech.Constants', 'ui.keypress', 'angularFileUpload'
    ])
    .config(function ($provide, $stateProvider, $urlRouterProvider, RestangularProvider, datepickerPopupConfig) {//,//, $datepickerProvider,,  

        'use strict';

        $provide.decorator('$state', function ($delegate, $stateParams) {
            $delegate.forceReload = function () {
                return $delegate.go($delegate.current, $stateParams, {
                    reload: true,
                    inherit: false,
                    notify: true
                });
            };
            return $delegate;
        });

        RestangularProvider
            .setBaseUrl('api/')
            .setErrorInterceptor(function (response) {
                console.log(response);
                if (response.status == 401) {
                    alert('You are not allowed to access this page');
                    //window.location = "/";
                }
            })
            .addResponseInterceptor(function (responseData, operation, what, url, response) {
                var extractedData = responseData;

                if (operation === "getList") {
                    extractedData.total = parseInt(response.headers().total, 10);
                }

                return extractedData;
            });

        $urlRouterProvider.otherwise('/certificate');

        $stateProvider
            .state('user', {
                url: '/user',
                templateUrl: '/Scripts/app/views/users.html',
                controller: 'userController'
            })
              .state('config', {
                  url: '/config',
                  templateUrl: '/Scripts/app/views/config.html',
                  controller: 'configController'
              })
            .state('user-item', {
                url: '/use/{id}',
                templateUrl: '/Scripts/app/views/user-item.html',
                controller: 'userItemController'
            })
            .state('certificate', {
                url: '/certificate?customerId&customerName&serialNo&certificateNo&pageIndex',
                templateUrl: '/Scripts/app/views/certificates.html',
                controller: 'certificateController'
            })
            .state('certificate-new', {
                url: '/certificate-new?model',
                templateUrl: '/Scripts/app/views/certificate-item.html',
                controller: 'certificateItemController'
            })
            .state('certificate-item', {
                url: '/certificate/{id}',
                templateUrl: '/Scripts/app/views/certificate-item.html',
                controller: 'certificateItemController',
                resolve: {
                    hasAccess: function ($stateParams, Constants, $timeout, $q, Areas, $state) {
                        var defer = $q.defer(),
                            certId = Areas.filter(function(area) {
                                return area.text == 'Certificate';
                            })[0].value;

                        if (!(Constants.accountInfo.areas & certId)) {
                            $timeout(function () {
                                $state.go('certificate-item-view', $stateParams);
                                return defer.reject('Not authorized');
                            });
                        } else {
                            defer.resolve();
                        }

                        return defer.promise;
                    }
                }
            })
            .state('certificate-item-view', {
                url: '/certificate/view/{id}',
                templateUrl: '/Scripts/app/views/certificate-item-view.html',
                controller: 'certificateItemViewController'

            })
            .state('quote-new', {
                url: '/quote-new?model',
                templateUrl: '/Scripts/app/views/quote-item.html',
                controller: 'quoteItemController'
            })
            .state('quote-item', {
                url: '/quote/{id}',
                templateUrl: '/Scripts/app/views/quote-item.html',
                controller: 'quoteItemController'
            })
            .state('quote', {
                url: '/quotes?customerId&customerName&pageIndex',
                templateUrl: '/Scripts/app/views/quotes.html',
                controller: 'quoteController'
            })
            .state('customer-new', {
                url: '/customer',
                templateUrl: '/Scripts/app/views/customer-item.html',
                controller: 'customerItemController'
            })
            .state('customer-item', {
                url: '/customer/{id}',
                templateUrl: '/Scripts/app/views/customer-item.html',
                controller: 'customerItemController'
            })
            .state('customer', {
                url: '/customers?name&pageIndex',
                templateUrl: '/Scripts/app/views/customers.html',
                controller: 'customerController'
            })
              .state('equipment-new', {
                  url: '/equipment?customerId&customerName',
                  templateUrl: '/Scripts/app/views/equipment-item.html',
                  controller: 'equipmentItemController'
              })
            .state('equipment-item', {
                url: '/equipment/{id}',
                templateUrl: '/Scripts/app/views/equipment-item.html',
                controller: 'equipmentItemController'
            })
            .state('equipment', {
                url: '/equipments?model&pageIndex',
                templateUrl: '/Scripts/app/views/equipments.html',
                controller: 'equipmentController',
                reloadOnSearch: false
            })
            // Standard
            .state('standard-new', {
                url: '/standard?model',
                templateUrl: '/Scripts/app/views/standard-item.html',
                controller: 'standardItemController'
            })
            .state('standard-item', {
                url: '/standard/{id}',
                templateUrl: '/Scripts/app/views/standard-item.html',
                controller: 'standardItemController'
            })
            .state('standard', {
                url: '/standards?standardNo&pageIndex',
                templateUrl: '/Scripts/app/views/standards.html',
                controller: 'standardController'
            })
            // Work Order
            .state('workOrder-new', {
                url: '/workOrder?model',
                templateUrl: '/Scripts/app/views/workOrder-item.html',
                controller: 'workOrderItemController'
            })
            .state('workOrder-item', {
                url: '/workOrder/{id}',
                templateUrl: '/Scripts/app/views/workOrder-item.html',
                controller: 'workOrderItemController'
            })
            .state('workOrder', {
                url: '/workOrders?customerId&customerName&pageIndex',
                templateUrl: '/Scripts/app/views/workOrder.html',
                controller: 'workOrderController'
            })
            //customerEquipment
          .state('customerEquipment-new', {
              url: '/customerEquipment?customerId&customerName',
              templateUrl: '/Scripts/app/views/customerEquipment-item.html',
              controller: 'customerEquipmentItemController'
          })
            .state('customerEquipment-item', {
                url: '/customerEquipment/{id}',
                templateUrl: '/Scripts/app/views/customerEquipment-item.html',
                controller: 'customerEquipmentItemController'
            })
            .state('customerEquipment', {
                url: '/customerEquipments?customerId&customerName&pageIndex',
                templateUrl: '/Scripts/app/views/customerEquipments.html',
                controller: 'customerEquipmentController',
                reloadOnSearch: false
            })
            // PO
            .state('po-new', {
                url: '/po-new?model',
                templateUrl: '/Scripts/app/views/po-item.html',
                controller: 'poItemController'
            })
            .state('po-item', {
                url: '/po/{id}',
                templateUrl: '/Scripts/app/views/po-item.html',
                controller: 'poItemController'
            })
            .state('po', {
                url: '/pos?customerId&customerName&pageIndex',
                templateUrl: '/Scripts/app/views/pos.html',
                controller: 'poController'
            })
        ;

        //angular.extend($datepickerProvider.defaults, {
        //    dateFormat: 'dd/MMM/yyyy',
        //    autoclose: true
        //});

        datepickerPopupConfig.dateFormat = 'dd/MMM/yyyy';
    })
    .run(function (editableOptions, $httpBackend, $rootScope, $window, $timeout) {
        'use strict';

        editableOptions.theme = 'bs3';

        $rootScope.back = function () {
            history.back();
        };

        $rootScope.doPrint = function () {
            $rootScope.title = '';
            $timeout(function () {
                $window.print();
                location.reload();
            });
        };
    })
    .filter('ordinal', function () {
        return function (input) {
            return input.charAt(0).toUpperCase() + input.substr(1).replace(/[A-Z]/g, ' $&');
        }
    });


